March 16, 2007 10:00 am
This is an attempt to keep a complete Mathematica notebook file for a session.

My goal for this session is to familiarize myself with the commands necessary to graph simple functions with complete control over the scale and domain as well as the color and labeling. In the case of two linear equations I want to see the solution expressed to an appropriate precision. Finally, I want to be able to do a simple linear regression for a set of values, and have this plotted.

First, a simple calculation.

In[1]:=

2 + 3

Out[1]=

5

I use alt-7 to set the Format for a cell to text (like this cell). Clicking below a cell creates a new cell. The default format for a new cell is the Input style, which means that Mathematica will try to evaluate the expression when the Enter key (or the shift-return key) is pressed.

All Mathematica names begin with a capital letter.
All arguments of a Mathematica command must be enclosed within square brackets.
The equal sign assigns a value to a name.

In[2]:=

x = 4

Out[2]=

4

In the above example, x is given a value of 4. To see the value of any name (i.e. variable), simply type the name of the variable and press Enter.

In[3]:=

x

Out[3]=

4

To clear the value of a variable type the name of the variable, an equal sign, and a period.

In[4]:=

x=.

In[5]:=

x

Out[5]=

x

Four common mathematical constants are Pi, E, I and Infinity (note that these are all Mathematica names). They will appear in symbolic form in mathematical expressions and in decimal form when they need to be evaluated. The decimal form is given by typing two slashes and the capital letter N after the name.

In[6]:=

{Pi, E, I, Infinity}

Out[6]=

{π, , , ∞}

In[7]:=

Pi//N

Out[7]=

3.14159

In[8]:=

E//N

Out[8]=

2.71828

In[12]:=

I//N

Out[12]=

0. + 1. 

In[13]:=

I^2//N

Out[13]=

-1.

In[14]:=

(I^2)//N

Out[14]=

-1.

In[15]:=

Infinity//N

Out[15]=

∞

Basic plotting is accomplished with the Plot command. The basic syntax is Plot[expression, {x, a, b}] which will plot the values of the expression where x takes values from a to b.

Plot[x^2 + 5, {x, -5, 5}]

[Graphics:HTMLFiles/index_25.gif]

Out[16]=

⁃Graphics⁃

Typing a semicolon after the command suppresses the Output line.

In[17]:=

Plot[x^2 + 5, {x, -5, 5}] ;

[Graphics:HTMLFiles/index_28.gif]

One often wants to superimpose two or more graphs on the same diagram. Suppose f1 and f2 are two functions.

In[18]:=

f1 = x^2 + 5

Out[18]=

5 + x^2

In[19]:=

f2 = 3x - 2

Out[19]=

-2 + 3 x

In[20]:=

Plot[{f1, f2}, {x, -5, 5}] ;

[Graphics:HTMLFiles/index_34.gif]

Suppose one wants to have the lines in different colors.

In[30]:=

p1 = Plot[f1, {x, -5, 5}, PlotStyleRed] ;

[Graphics:HTMLFiles/index_36.gif]

In[31]:=

p2 = Plot[f2, {x, -5, 5}, PlotStyleBlue] ;

[Graphics:HTMLFiles/index_38.gif]

In[32]:=

Show[p1, p2] ;

[Graphics:HTMLFiles/index_40.gif]

Mathematica has 30 different options for the Plot command. So far we have seen one, PlotStyle, which can be used to specify the color of the curve.
Another useful option is called AspectRatio. This controls the ratio of the height to the width of the plot.
     If it is set to 1/GoldenRatio then one sees an aesthetically pleasing form.
     If it is set to Automatic then both axes are set to the same scale.
     If one wants to avoid difficulties when Automatic gives too narrow a plot, try using PlotRange and restrict the range to smaller values.

In[38]:=

p3 = Plot[x^2 + 3, {x, -10, 10}, PlotStyleRed] ;

[Graphics:HTMLFiles/index_42.gif]

In[35]:=

p3 = Plot[x^2 + 3, {x, -10, 10}, PlotStyleRed, AspectRatio1/GoldenRatio] ;

[Graphics:HTMLFiles/index_44.gif]

In[36]:=

p3 = Plot[x^2 + 3, {x, -10, 10}, PlotStyleRed, AspectRatioAutomatic] ;

[Graphics:HTMLFiles/index_46.gif]

In[39]:=

p3 = Plot[x^2 + 3, {x, -10, 10}, PlotStyleRed, AspectRatioAutomatic, PlotRange {0, 20}] ;

[Graphics:HTMLFiles/index_48.gif]

In[41]:=

p3 = Plot[x^2 + 3, {x, -10, 10}, PlotStyleRed, AspectRatioAutomatic, PlotRange {0, 10}] ;

[Graphics:HTMLFiles/index_50.gif]

AxesLabel is used to label the axes.

In[44]:=

p3 = Plot[x^2 + 3, {x, -10, 10}, PlotStyleRed, AspectRatioAutomatic, PlotRange {0, 20}, AxesLabel {x, y}] ;

[Graphics:HTMLFiles/index_52.gif]

One can also change the size of a graph by selecting it and then dragging the sides to an appropriate size. The aspect ratio will stay the same.

[Graphics:HTMLFiles/index_53.gif]

Now lets try a simple regression analysis, given a set of points.

Larson, p. 19, #74
An instructor gives regular 20-point quizzes and 100-point exams in a mathematics course. Average scores for the six students, given as ordered pairs (x,y) where x is the average quiz score and y is the average test score, are (18,87), (10,55), (19,96), (16,79), (13,76), (15,82).
(a) Use the regression capabilities of a graphing utility to find the least squares regression line for the data.
(b) Use the graphing utility to plot the points and graph the regression line in the same viewing rectangle.
(c) Use the regression line to predict the average exam score for a student with an average quiz score of 17.
(d) Interpret the meaning of the slope of the regression line.
(e) If the instructor added 4 points to the average test score of everyone in the class, describe the change in the position of the plotted points  and the change in the equation of the line.

data = {{18, 87}, {10, 55}, {19, 96}, {16, 79}, {13, 76}, {15, 82}} ;

In[57]:=

pdata = ListPlot[data, PlotStyleAbsolutePointSize[4]] ;

[Graphics:HTMLFiles/index_56.gif]

Now to begin.
(a) Find the regression line for this data.

In[61]:=

eq = Fit[data, {1, x}, x]

Out[61]=

18.9149 + 3.97264 x

(b)  Use the graphing utility to plot the points and graph the regression line in the same viewing rectangle.

In[66]:=

pline = Plot[eq, {x, 0, 20}, PlotStyleRed] ;

[Graphics:HTMLFiles/index_60.gif]

In[67]:=

Show[{pdata, pline}] ;

[Graphics:HTMLFiles/index_62.gif]

(c) Use the regression line to predict the average exam score for a student with an average quiz score of 17.

In[70]:=

y = 18.9149 + 3.97264 * 17

Out[70]=

86.4498

The predicted score is 86.

(d) The slope of the regression line gives the ratio of an increase in quiz scors to that of test scores. The slope of 3.97 says that for every gain of 1 point in the quiz scores, one should have an increase of 4 points in the test scores. This also shows that the test scores are slightly harder than the quiz scores, since if they were the same difficulty then one would expect a gain of 5 points in the test scores.

(e) If the instructor added 4 points to the average test score of everyone in the class, describe the change in the position of the plotted points  and the change in the equation of the line.

All points would be 4 points higher. The slope of the regression line would remain the same, but the constant would be 4 points larger.

12:15 PM The above activity took 2 hours and 15 minutes. I now have a template that I can refer to for problems involving simple equations and their graphs as well as simple linear regressions.


Created by Mathematica  (March 16, 2007) Valid XHTML 1.1!